//
//  CYWoShiChuangYeZheViewController.m
//  cyydt
//
//  Created by chu on 2019/2/6.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYWoShiChuangYeZheViewController.h"
#import "CYEditNicknameViewController.h"
#import "CYAddressModel.h"
#import "CYMinePickerView.h"
#import "CYSelCategaryView.h"
#import "SYPersonalNotViewController.h"
#import "CYChuangYeZheTableViewCell.h"

@interface CYWoShiChuangYeZheViewController ()<UITableViewDelegate, UITableViewDataSource, CYEditNicknameDelegate>
{
    NSArray *_titles;
    
    NSString *_name;
    NSString *_phone;
    NSNumber *_ageId;
    NSString *_provinceId;
    NSString *_cityId;
    NSNumber *_zhuangtaiId;
    NSNumber *_industryId;
    NSNumber *_subIndustryId;
    NSNumber *_directionId;
    NSNumber *_budgetId;

    
    NSArray *_zhuangtaiArr;
    NSArray *_jineArr;
    NSArray *_fangxiangArr;
    NSArray *_nianlingArr;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *rightArr;

@property (nonatomic, strong) NSMutableArray *rightContentArr;

@property (nonatomic, strong) NSMutableArray *addressArr;

@property (nonatomic, strong) NSMutableArray *categaryArr;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) UIView *footerView;

@end

@implementation CYWoShiChuangYeZheViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _titles = @[@"姓名", @"电话", @"年龄", @"地区", @"创业状态", @"行业爱好", @"创业方向", @"投资预算"];
    [self getAddress];
    [self getAllCategary];
    //创业状态
    [self getStagesWithType:@"4"];
    //投资金额
    [self getStagesWithType:@"2"];
    //创业方向
    [self getStagesWithType:@"5"];
    //创业年龄
    [self getStagesWithType:@"3"];
    self.title = @"创业者";
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CYChuangYeZheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYChuangYeZheTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYChuangYeZheTableViewCell" owner:self options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.leftLabel.text = _titles[indexPath.row];
    cell.contentTF.placeholder = self.rightArr[indexPath.row];
    cell.contentTF.text = self.rightContentArr[indexPath.row];
    cell.contentTF.userInteractionEnabled = NO;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 12, 2, 16)];
    view1.backgroundColor = [UIColor redColor];
    [view addSubview:view1];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(view1.frame) + 10, 12, 100, 16)];
    label.textColor = RGBOF(0x333333);
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"填写资料";
    [view addSubview:label];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 49, kWindowW, 1)];
    view2.backgroundColor = RGBOF(0xE3E3E3);
    [view addSubview:view2];

    return view;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger tag = indexPath.row;
    switch (tag) {
        case 0:
        {
            //姓名
            CYEditNicknameViewController *edit = [[CYEditNicknameViewController alloc] init];
            edit.title = @"姓名";
            edit.type = EditType_areaName;
            edit.delegate = self;
            [[AppDelegate sharedAppDelegate] pushViewController:edit];
        }
            break;
        case 1:
        {
            //电话
            CYEditNicknameViewController *edit = [[CYEditNicknameViewController alloc] init];
            edit.title = @"手机号";
            edit.delegate = self;
            edit.type = EditType_phone;
            [[AppDelegate sharedAppDelegate] pushViewController:edit];
        }
            break;
        case 2:
        {
            //年龄
            [self selNianLing];
            
        }
            break;
        case 3:
        {
            //地域
            [self showAddress];
        }
            break;
        case 4:
        {
            //创业状态
            [self selChuangYeZhuangTai];
        }
            break;
        case 5:
        {
            //行业爱好
            [self selHangYe];
        }
            break;
        case 6:
        {
            //创业方向
            [self selChuangYeFangXiang];
        }
            break;
        case 7:
        {
            //投资预算
            [self selTouZiYuSuan];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -
- (void)saveWithValue:(NSString *)value EditType:(EditType)editType{
    if (editType == EditType_areaName) {
        _name = value;
        [self.rightContentArr replaceObjectAtIndex:0 withObject:value];
    }else{
        _phone = value;
        [self.rightContentArr replaceObjectAtIndex:1 withObject:value];
    }
    [self.tableView reloadData];
}

#pragma mark - 地址选择
- (void)showAddress{
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:self.addressArr components:2];
    headView.titleLable.text = @"请选择地区";
    headView.addressBlock = ^(CYAddressModel * _Nonnull province, CYCityModel * _Nonnull city) {
        NSLog(@"province.name -- %@",province.name);
        NSLog(@"city -- %@",city.name);
        self->_provinceId = province.areaid;
        self->_cityId = city.areaid;
        [self.rightContentArr replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%@-%@",province.name,city.name]];
        [self.tableView reloadData];
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 选择创业年龄
- (void)selNianLing{
    NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _nianlingArr) {
        [muArr addObject:dic[@"name"]];
    }
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:muArr components:1];
    headView.titleLable.text = @"请选择年龄";
    headView.otherBlock = ^(NSString * _Nonnull value, NSInteger index) {
        NSDictionary *valueDic = self->_nianlingArr[index];
        self->_ageId = valueDic[@"id"];
        [self.rightContentArr replaceObjectAtIndex:2 withObject:valueDic[@"name"]];
        [self.tableView reloadData];
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 选择创业状态
- (void)selChuangYeZhuangTai{
    NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _zhuangtaiArr) {
        [muArr addObject:dic[@"name"]];
    }
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:muArr components:1];
    headView.titleLable.text = @"请选择创业状态";
    headView.otherBlock = ^(NSString * _Nonnull value, NSInteger index) {
        
        NSDictionary *valueDic = self->_zhuangtaiArr[index];
        self->_zhuangtaiId = valueDic[@"id"];
        [self.rightContentArr replaceObjectAtIndex:4 withObject:valueDic[@"name"]];
        [self.tableView reloadData];
        
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 选择感兴趣的行业
- (void)selHangYe{
    NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYSelCategaryView" owner:self options:nil];
    CYSelCategaryView *backView = [nibView objectAtIndex:0];
//    backView.haveSmallCategary = YES;
    backView.frame = CGRectMake(0, 0, kWindowW, 410);
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    backView.dataSourceArr = self.categaryArr;
    backView.block = ^(CYCategrayModel * _Nonnull bigCateModel, CYCategrayModel * _Nonnull smallCateModel) {
        self->_industryId = bigCateModel.catid;
        self->_subIndustryId = smallCateModel.catid;
        [self.rightContentArr replaceObjectAtIndex:5 withObject:bigCateModel.catname];
        [self.tableView reloadData];
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:backView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 选择创业方向
- (void)selChuangYeFangXiang{
    NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _fangxiangArr) {
        [muArr addObject:dic[@"name"]];
    }
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:muArr components:1];
    headView.titleLable.text = @"请选择创业方向";
    headView.otherBlock = ^(NSString * _Nonnull value, NSInteger index) {
        NSDictionary *valueDic = self->_fangxiangArr[index];
        self->_directionId = valueDic[@"id"];
        [self.rightContentArr replaceObjectAtIndex:6 withObject:valueDic[@"name"]];
        [self.tableView reloadData];
        
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 选择投资预算
- (void)selTouZiYuSuan{
    NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _jineArr) {
        [muArr addObject:dic[@"name"]];
    }
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:muArr components:1];
    headView.titleLable.text = @"请选择投资预算";
    headView.otherBlock = ^(NSString * _Nonnull value, NSInteger index) {
        NSDictionary *valueDic = self->_jineArr[index];
        self->_budgetId = valueDic[@"id"];
        [self.rightContentArr replaceObjectAtIndex:7 withObject:valueDic[@"name"]];
        [self.tableView reloadData];
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}


#pragma mark - 获取地址
- (void)getAddress{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"areas"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取地址 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    [self.addressArr removeAllObjects];
                    for (NSDictionary *dic in data) {
                        CYAddressModel *model = [CYAddressModel mj_objectWithKeyValues:dic];
                        [self.addressArr addObject:model];
                    }
                }
            }else{
                
            }
        }
    }];
}

#pragma mark - 获取创业属性
- (void)getStagesWithType:(NSString *)type{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"stages"];
    NSDictionary *param = @{@"type":type};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取创业属性 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    if ([type isEqualToString:@"2"]) {
                        //投资金额
                        self->_jineArr = data;
                    }else if ([type isEqualToString:@"3"]){
                        self->_nianlingArr = data;
                    }else if ([type isEqualToString:@"4"]){
                        self->_zhuangtaiArr = data;
                    }else if ([type isEqualToString:@"5"]){
                        self->_fangxiangArr = data;
                    }
                }
            }else{
                
            }
        }
    }];
}

#pragma mark - 获取所有分类
- (void)getAllCategary{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"categories"];
    NSDictionary *param = @{@"type":@3};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取所有分类 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    [self.categaryArr removeAllObjects];
                    for (NSDictionary *dic in data) {
                        NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
                        [muDic setObject:dic[@"name"] forKey:@"catname"];
                        [muDic setObject:dic[@"id"] forKey:@"catid"];
                        [muDic setObject:dic[@"childCates"] forKey:@"child"];
                        [muDic removeObjectForKey:@"id"];
                        [muDic removeObjectForKey:@"name"];
                        [muDic removeObjectForKey:@"childCates"];
                        CYCategrayModel *model = [CYCategrayModel modelWithDictionary:muDic];
                        [self.categaryArr addObject:model];
                    }
                }
            }else{
                
            }
        }
    }];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"CYChuangYeZheTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYChuangYeZheTableViewCell"];
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

- (NSMutableArray *)addressArr{
    if (!_addressArr) {
        _addressArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _addressArr;
}

- (NSMutableArray *)categaryArr{
    if (!_categaryArr) {
        _categaryArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _categaryArr;
}

- (NSMutableArray *)rightArr{
    if (!_rightArr) {
        _rightArr = [NSMutableArray arrayWithCapacity:0];
        [_rightArr addObject:@"请输入您的姓名"];
        [_rightArr addObject:@"请输入您的手机号"];
        [_rightArr addObject:@"请选择您的年龄"];
        [_rightArr addObject:@"请选择您所在的地域"];
        [_rightArr addObject:@"请选择目前创业状态"];
        [_rightArr addObject:@"请选择行业"];
        [_rightArr addObject:@"请选择您的创业方向"];
        [_rightArr addObject:@"请选择您的投资预算"];
    }
    return _rightArr;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 125 * kWindowWHOne)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 115 * kWindowWHOne)];
        imageView.image = [UIImage imageNamed:@"cyz_bg"];
        [_headerView addSubview:imageView];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 84)];
        UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 20, kWindowW - 30, 44)];
        [doneBtn setTitle:@"提交" forState:UIControlStateNormal];
        [doneBtn setBackgroundColor:RGBOF(0xFF5353)];
        doneBtn.layer.cornerRadius = 22;
        doneBtn.layer.masksToBounds = YES;
        [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [doneBtn addTarget:self action:@selector(tijiaoAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:doneBtn];
    }
    return _footerView;
}

- (void)tijiaoAction:(UIButton *)sender{
    if ([NSString stringIsNull:_name]) {
        [self.view makeToast:@"请填写您的姓名" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:_phone]) {
        [self.view makeToast:@"请填写您的手机号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_ageId) {
        [self.view makeToast:@"请选择您的年龄" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:_provinceId]) {
        [self.view makeToast:@"请选择您的地址" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_zhuangtaiId) {
        [self.view makeToast:@"请选择您的创业状态" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_industryId) {
        [self.view makeToast:@"请选择您的行业" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_directionId) {
        [self.view makeToast:@"请选择您的创业方向" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_budgetId) {
        [self.view makeToast:@"请选择您的投资预算" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"user/estp"];
    NSDictionary *param = @{@"username":_name, @"phone":_phone, @"age":_ageId, @"province":_provinceId, @"city":_cityId, @"type":_zhuangtaiId, @"industry":_industryId, @"subIndustry":_subIndustryId, @"direction":_directionId, @"budget":_budgetId};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"我是创业者 ---- %@",responseResult);
        
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                [[AppDelegate sharedAppDelegate] popViewController];
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (NSMutableArray *)rightContentArr{
    if (!_rightContentArr) {
        _rightContentArr = [NSMutableArray arrayWithCapacity:0];
        [_rightContentArr addObject:@""];
        [_rightContentArr addObject:@""];
        [_rightContentArr addObject:@""];
        [_rightContentArr addObject:@""];
        [_rightContentArr addObject:@""];
        [_rightContentArr addObject:@""];
        [_rightContentArr addObject:@""];
        [_rightContentArr addObject:@""];
    }
    return _rightContentArr;
}

@end
